//
//  ShareKit.m
//  Malena
//
//  Created by Dawn on 12-11-10.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "ShareKit.h"
#import <Social/Social.h>
#import <ACCOUNTS/ACAccount.h>

static ShareKit *shareKit;
@implementation ShareKit
@synthesize contentViewController = _contentViewController;

+ (id) share{
    if (!shareKit) {
        shareKit = [[ShareKit alloc] init];
    }
    return shareKit;
}


- (UIImage *)addWatermarkToImage:(UIImage *)image{
    UIGraphicsBeginImageContext(image.size);
    
    // Draw image
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    
    // Draw watermark
    UIImage *watermark = [UIImage imageNamed:@"Watermark.png"];
    [watermark drawInRect:CGRectMake(10, image.size.height - watermark.size.height - 10, watermark.size.width, watermark.size.height)];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultingImage;
}

- (BOOL) shareByMailWithTitle:(NSString *)title content:(NSString *)content image:(UIImage *)image{
    Class mailClass = NSClassFromString(@"MFMailComposeViewController");
    if(mailClass != nil){
        if([mailClass canSendMail]){
            image = [self addWatermarkToImage:image];
            NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
            [self displayMailComposerWithTitle:title content:content imageData:imageData];
            return YES;
        }
        else {
            NSURL *url = [NSURL URLWithString:@"mailto:"];
            if([[UIApplication sharedApplication] canOpenURL:url]){
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Mail has not set",nil)
                                                                    message:NSLocalizedString(@"Please set the mail account before sharing", nil)
                                                                   delegate:self cancelButtonTitle:NSLocalizedString(@"Later", nil)
                                                          otherButtonTitles:NSLocalizedString(@"Setting", nil),nil];
                [alertView show];
                [alertView release];
            }else {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Tips", nil)
                                                                    message:NSLocalizedString(@"Your mail is not Available now", nil)
                                                                   delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil)
                                                          otherButtonTitles:nil];
                [alertView show];
                [alertView release];
            }
        
            if (self.contentViewController) {
                [self.contentViewController dismissModalViewControllerAnimated:YES];
            }
            return NO;
        }
    }else{
        if (self.contentViewController) {
            [self.contentViewController dismissModalViewControllerAnimated:YES];
        }
        return NO;
    }

}


- (void) shareBySystemKitWithContent:(NSString *)content image:(UIImage *)image{
    
    NSArray *activityItems;
    
    image = [self addWatermarkToImage:image];
    if (image!= nil) {
        activityItems = @[content, image];
    } else {
        activityItems = @[content];
    }
    
    UIActivityViewController *activityController =
    [[UIActivityViewController alloc] initWithActivityItems:activityItems
                                      applicationActivities:nil];
    
    activityController.completionHandler = ^(NSString *activityType, BOOL completed){
        if (IOSVersion_5) {
            [self.contentViewController willRotateToInterfaceOrientation:self.contentViewController.interfaceOrientation duration:0];
        }
    };
    

    
    [self.contentViewController presentModalViewController:activityController animated:YES];
    [activityController release];
}

-(void) displayMailComposerWithTitle:(NSString *)title content:(NSString *)content imageData:(NSData *)imageData{
	[title retain];
    [content retain];
    [imageData retain];

    MFMailComposeViewController *mailComposeVC = [[MFMailComposeViewController alloc] init];
	mailComposeVC.mailComposeDelegate = self;
	[mailComposeVC setSubject:title];
	[mailComposeVC setMessageBody:content isHTML:NO];
	[mailComposeVC addAttachmentData:imageData mimeType:@"image/jpeg" fileName:SHAREIMAGE_NAME];
	mailComposeVC.modalPresentationStyle = UIModalPresentationFormSheet;
	if (self.contentViewController) {
        [self.contentViewController presentModalViewController:mailComposeVC animated:YES];
    }
	[mailComposeVC release];
    
    [title release];
    [content release];
    [imageData release];
}

#pragma mark -
#pragma mark mailCompose delegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
	NSString *messageResult = @"";
	if(result == MFMailComposeResultFailed){
		messageResult = NSLocalizedString(@"Mail send failed",nil);
		UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil
														   message:messageResult
														  delegate:nil
												 cancelButtonTitle:NSLocalizedString(@"OK",nil)
												 otherButtonTitles:nil];
		[alertView show];
		[alertView release];
	}
	else if(result == MFMailComposeResultSent){
		messageResult = NSLocalizedString(@"Mail sent successfully",nil);
		UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil
														   message:messageResult
														  delegate:nil
												 cancelButtonTitle:NSLocalizedString(@"OK",nil)
												 otherButtonTitles:nil];
		[alertView show];
		[alertView release];
	}
    
    if (self.contentViewController) {
        if (IOSVersion_5) {
            [self.contentViewController willRotateToInterfaceOrientation:self.contentViewController.interfaceOrientation duration:0];
        }
        [self.contentViewController dismissModalViewControllerAnimated:YES];
    }
}

@end
